package org.testis.webautomationmodule.ui.objects;

import org.testis.data.AtofModuleConstants;
import org.testis.logging.AtofLogLevel;
import org.testis.webautomationmodule.ui.UIFormFieldDecorator;


public class UICheckBox extends UIFormFieldDecorator
{

    public UICheckBox(String id)
    {
        super(id);
    }


    public UICheckBox(String id,
                      String value)
    {
        super(id,
              value);
    }


    @Override
    protected boolean setBehavior()
    {
        if (checkAvailableForAction()) {
            String val = getWebAutomationImpl().getValue(getId());
            if (getValue().equals(
                AtofModuleConstants.SET_CHECK_BOX_ON_VALUE)) {
                if (!val.equals(AtofModuleConstants.CHECK_BOX_ON_VALUE)) {
                    getWebAutomationImpl().click(getId());
                    putLog(" checking the check box with id " + getId());
                }
                else {
                    putLog(" check box already checked with id " + getId(),AtofLogLevel.WARN);
                }
            }
            else if (getValue().equals(
                AtofModuleConstants.SET_CHECK_BOX_OFF_VALUE)) {
                if (val.equals(AtofModuleConstants.CHECK_BOX_ON_VALUE)) {
                    getWebAutomationImpl().click(getId());
                    putLog(" unchecking the check box with id " + getId());
                }
                else {
                    putLog(" check box already unchecked with id " + getId(),AtofLogLevel.WARN);
                }
            }
            else {
                putLog(" Invalid value entered for checkbox " + getId(),AtofLogLevel.ERROR);
                return false;
            }
            return true;
        }
        else {
            putLog(" Checkbox was not on editable state " + getId(),AtofLogLevel.ERROR);
            return false;
        }
    }


    @Override
    public boolean verifyBehavior()
    {
        boolean result = false;
        String curValue = AtofModuleConstants.SET_CHECK_BOX_OFF_VALUE;
        if (checkAvailableForVerification()) {

            String val = getWebAutomationImpl().getValue(getId());
            if (val.equals(AtofModuleConstants.CHECK_BOX_ON_VALUE)) {
                curValue = AtofModuleConstants.SET_CHECK_BOX_ON_VALUE;
            }
            if (getValue().equals(curValue)) {
                putLog(" Object with " + getId() +
                       " id was  verified... curValue:" + curValue +
                       " compared with:" +
                       getValue());
                result = true;
            }
            else {
                putLog(" Object with " + getId() +
                       " id was not verified... curValue:" + curValue +
                       " compared with:" +
                       getValue(),AtofLogLevel.ERROR);
                result = false;
            }
            setValue(curValue);
        }
        else {
            putLog(" Object with " + getId() +
                   " id was not available for verification...",AtofLogLevel.ERROR);
            setValue(null);
        }

        return result;
    }


    @Override
    public boolean getBehavior()
    {
        if (checkAvailableForVerification()) {
            String curValue =
                AtofModuleConstants.SET_CHECK_BOX_OFF_VALUE;
            String val = getWebAutomationImpl().getValue(getId());
            if (val.equals(AtofModuleConstants.CHECK_BOX_ON_VALUE)) {
                curValue = AtofModuleConstants.SET_CHECK_BOX_ON_VALUE;
            }
            setValue(curValue);
        }
        else {
            putLog(" Checkbox was not on editable state " + getId(),AtofLogLevel.ERROR);
            setValue(null);
        }
        putLog("Gets value of UICheckBox with id=" +
               getId() + " value=" + getValue());
        return true;
    }
}

